﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using DAL;

namespace DACNPM.MyUserControl.UCHienThi
{
    public partial class UCDanhSachNV : UserControl
    {
        NhanVienBUO nvBUO = new NhanVienBUO();

        public String idNV;
        int id;
        public UCDanhSachNV()
        {
            InitializeComponent();
            dataGridView1.DataSource = nvBUO.GetAll();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (rdTimTheoTen.Checked == true)
            {
                if (txtTimTheoTen.Text.Trim().Equals(""))
                {
                    MessageBox.Show("Chưa nhập tên nhân viên cần tìm");
                }
                else
                {
                    List<tbl_NhanVien> list = nvBUO.searchByTenNV(txtTimTheoTen.Text.Trim());

                    if (list.Count > 0)
                    {
                        dataGridView1.AutoGenerateColumns = false;
                        dataGridView1.DataSource = list;
                        dataGridView1.Visible = true;
                    }
                    else
                    {
                        dataGridView1.Visible = true;
                        MessageBox.Show("Không có nhân viên cần tìm");
                    }
                }
            }
            else
            {
                if (txtTimTheoMaNV.Text.Trim().Equals(""))
                {
                    MessageBox.Show("Chưa nhập mã nhân viên cần tìm");
                }
                else
                {
                    List<tbl_NhanVien> list = nvBUO.searchByMaNV(txtTimTheoMaNV.Text.Trim());

                    if (list.Count > 0)
                    {
                        dataGridView1.AutoGenerateColumns = false;
                        dataGridView1.DataSource = list;
                        dataGridView1.Visible = true;
                    }
                    else
                    {
                        dataGridView1.Visible = false;
                        MessageBox.Show("Không có nhân viên cần tìm");
                    }
                }
            }
        }

        private void txtTimTheoTen_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        private void txtTimTheoTen_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (rdTimTheoTen.Checked == true)
                {
                    if (txtTimTheoTen.Text.Trim().Equals(""))
                    {
                        MessageBox.Show("Chưa nhập tên nhân viên cần tìm");
                    }
                    else
                    {
                        List<tbl_NhanVien> list = nvBUO.searchByTenNV(txtTimTheoTen.Text.Trim());

                        if (list.Count > 0)
                        {
                            dataGridView1.AutoGenerateColumns = false;
                            dataGridView1.DataSource = list;
                            dataGridView1.Visible = true;
                        }
                        else
                        {
                            dataGridView1.Visible = true;
                            MessageBox.Show("Không có nhân viên cần tìm");
                        }
                    }
                }
            }
        }

        private void rdTimTheoTen_CheckedChanged(object sender, EventArgs e)
        {   
                txtTimTheoTen.Enabled = true;
                txtTimTheoMaNV.Enabled = false;
                txtTimTheoTen.Text = "";
                txtTimTheoMaNV.Text = "";
                dataGridView1.Visible = true;
                dataGridView1.DataSource = nvBUO.GetAll();
        }

        private void rdTimMaNV_CheckedChanged(object sender, EventArgs e)
        {
                txtTimTheoTen.Enabled = false;
                txtTimTheoMaNV.Enabled = true;
                txtTimTheoTen.Text = "";
                txtTimTheoMaNV.Text = "";
                dataGridView1.Visible = true;
                dataGridView1.DataSource = nvBUO.GetAll();
        }

        private void txtTimTheoMaNV_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                if (txtTimTheoMaNV.Text.Trim().Equals(""))
                {
                    MessageBox.Show("Chưa nhập mã nhân viên cần tìm");
                }
                else
                {
                    List<tbl_NhanVien> list = nvBUO.searchByMaNV(txtTimTheoMaNV.Text.Trim());
                    if (list.Count > 0)
                    {
                        dataGridView1.AutoGenerateColumns = false;
                        dataGridView1.DataSource = list;
                        dataGridView1.Visible = true;
                    }
                    else
                    {
                        dataGridView1.Visible = false;
                        MessageBox.Show("Không có nhân viên cần tìm");
                    }
                }
            }
        }

        private void txtTimTheoTen_TextChanged(object sender, EventArgs e)
        {
            List<tbl_NhanVien> list = nvBUO.searchByTenNV(txtTimTheoTen.Text.Trim());

            if (list.Count > 0)
            {
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = list;
                dataGridView1.Visible = true;
            }
        }

        private void txtTimTheoMaNV_TextChanged(object sender, EventArgs e)
        {
            List<tbl_NhanVien> list = nvBUO.searchByMaNV(txtTimTheoMaNV.Text.Trim());

            if (list.Count > 0)
            {
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = list;
                dataGridView1.Visible = true;
            }
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {

                    DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                    // Only RowHeader and Cells (which make up a row) should fire a menu
                    if (hit.Type == DataGridViewHitTestType.RowHeader
                        || hit.Type == DataGridViewHitTestType.Cell
                        && hit.RowIndex >= 0)
                    {
                        // Notice how I assign the menu to the dataGridView below...

                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[hit.RowIndex].Selected = true;
                        dataGridView1.ContextMenuStrip = contextMenuStrip1;
                        dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));

                        id = dataGridView1.HitTest(e.X, e.Y).RowIndex;
                        idNV = dataGridView1.Rows[id].Cells[0].Value.ToString();


                    }
                    // and then I remove the menu here...
                    // This is because after we assign it to the dataGridView
                    // it allows you to right-click anywhere on the control to
                    // get a menu.  Try removing this line and expirement.
                    dataGridView1.ContextMenuStrip = null;

                }
            }
            catch
            {
                contextMenuStrip1.Visible = false;
            }
        }

        private void thêmNhânViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCThemNV ucThemNV = new UCThemNV();

            Form form = this.ParentForm;
            ControlAccess.ShowConTrol(form, ucThemNV, "grbHienThiChucNang");
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //idNV = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
        }

        private void sửaNhânViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCSuaNV ucSuaNV = new UCSuaNV(idNV);

            Form form = this.ParentForm;
            ControlAccess.ShowConTrol(form, ucSuaNV, "grbHienThiChucNang");
        }
    }
}
